package com.niiwoo.civet.trade.service.local.newRefund.notify.dubbo;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.activity.stage.dto.request.prize.ActivityPrizeRefinancingReqDTO;
import com.niiwoo.activity.stage.service.prize.ActivityPrizeDubboService;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dto.ProjectBorrowerAmountSumDTO;
import com.niiwoo.civet.trade.service.local.newRefund.common.RefundCommonService;
import com.niiwoo.civet.trade.service.local.newRefund.notify.RefundNotifyHandle;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyHandleResult;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;
import com.niiwoo.tripod.provider.exception.BizException;

import lombok.extern.slf4j.Slf4j;

/**
 * 活动中心借新还旧结清后返现事件
 * 
 * 触发条件：标的结清
 * 
 * @author: kevin.jia
 * @version 1.0.0
 */
@Slf4j
@Component
public class ActivityExchangeReturnCashNotify implements RefundNotifyHandle {

	@Autowired
	private RefundCommonService refundCommonServie;

	@Reference
	private ActivityPrizeDubboService activityPrizeDubboService;
	
	@Override
	public RefundNotifyHandleResult handle(RefundNotifyDTO refundNotifyDTO) {
		RefundNotifyHandleResult refundNotifyHandleResult = null;
        try {
        	ActivityPrizeRefinancingReqDTO activityPrizeRefinancingReqDTO = (ActivityPrizeRefinancingReqDTO) bulidParam(refundNotifyDTO);
        	
        	log.info("标的结清后调用活动中心，调用活动中心发放借新还旧返现红包，ProjectId：{}, 请求参数：{}", refundNotifyDTO.getProject().getProjectId(), JSONObject.toJSONString(activityPrizeRefinancingReqDTO));
    		activityPrizeDubboService.addRefinancingUserPrize(activityPrizeRefinancingReqDTO);
    		log.info("标的结清后调用活动中心，调用活动中心发放借新还旧返现红包完成, ProjectId：{}", refundNotifyDTO.getProject().getProjectId());
    		refundNotifyHandleResult = new RefundNotifyHandleResult();
		} catch (Exception e) {
			if (e instanceof BizException) {
				if (!"TRD00000".equals(((BizException) e).getErrorCode())) {
					refundNotifyHandleResult = new RefundNotifyHandleResult(e.getMessage());	
				} else {
					refundNotifyHandleResult = new RefundNotifyHandleResult();
				}
			} else {
				refundNotifyHandleResult = new RefundNotifyHandleResult(e.getMessage());
			}
			log.error("标的结清后调用活动中心，调用活动中心发放借新还旧返现红包异常，异常原因：{}, 请求参数：{}", e.getMessage(), JSONObject.toJSONString(refundNotifyDTO.getRefundNotifyRecord()), e);
		}
        return refundNotifyHandleResult;
	}

	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		Project project = refundNotifyDTO.getProject();
		
		// 累计标的费用
		ProjectBorrowerAmountSumDTO projectBorrowerAmountSumDTO = refundCommonServie
				.sumProjectBorrowerAmountByProjectId(refundNotifyDTO.getProject().getProjectId());
		if (projectBorrowerAmountSumDTO == null) {
			throw new BizException("TRD00000", "调用活动中心发放借新还旧返现红包异常，标的还款详情不存在"); 
		}
		
		// 返现金额 = 担保服务费 + 平台管理费
		BigDecimal returnCashAmount = projectBorrowerAmountSumDTO.getManagementFee().add(projectBorrowerAmountSumDTO.getGuaranteeFee());
		if (returnCashAmount.compareTo(BigDecimal.ZERO) <= 0) {
			throw new BizException("TRD00000", "标的结清后调用活动中心，返现金额小于或等于零，不做处理");
		}
		
		ActivityPrizeRefinancingReqDTO activityPrizeRefinancingReqDTO = new ActivityPrizeRefinancingReqDTO();
		activityPrizeRefinancingReqDTO.setProjectId(project.getProjectId());
		activityPrizeRefinancingReqDTO.setUserId(project.getBorrowUserId());
		activityPrizeRefinancingReqDTO.setAmount(returnCashAmount);
		
		return activityPrizeRefinancingReqDTO;
	}
	
	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.ACTIVITY_EXCHANGE_RETURN_CASH;
	}	
}